/*
 * <p>Copyright® 2009-2022 东软 IT 实训中心版权所有。</p>
 */

package com.max.back.doctor.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.max.back.entity.Registration;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author 辛凤文 forward.seen@foxmail.com
 * @version 1.0
 * @description <p>
 * <code>RegistrationDao</code>
 *
 * </p>
 * @time 2022/7/20 11:55
 * @see
 */
@Repository
public interface RegistrationDao extends BaseMapper<Registration> {

    @Select({
            "<script>",
            "SELECT count(*) FROM REGISTRATION r WHERE r.DELETED = 0 AND r" +
                    ".DOCTOR_ID= #{doctorId}",
            "</script>",
    })
    int queryCountForDoctor(@Param("doctorId") String doctorId);

    @Select({
            "<script>",
            "SELECT \n" +
                    "\tr.ID id,\n" +
                    "\tp.NAME name,\n" +
                    "\tp.AGE age,\n" +
                    "\t(\n" +
                    "\t\tSELECT\n" +
                    "\t\t\tV \n" +
                    "\t\tFROM\n" +
                    "\t\t\tCODE_NAME\n" +
                    "\t\tWHERE\n" +
                    "\t\t\tCODE_NAME.K = p.SEX\n" +
                    "\t) sex,\n" +
                    "\t\t(\n" +
                    "\t\tSELECT\n" +
                    "\t\t\tV \n" +
                    "\t\tFROM\n" +
                    "\t\t\tCODE_NAME\n" +
                    "\t\tWHERE\n" +
                    "\t\t\tCODE_NAME.K = r.REGISTER_STATUS\n" +
                    "\t) status,\n" +
                    "\tr.GMT_CREATE time\n" +
                    "FROM \n" +
                    " REGISTRATION r,\n" +
                    " PATIENT p\n" +
                    "WHERE\n" +
                    "\t(\n" +
                    "\tr.DELETED = 0\n" +
                    "\tAND \n" +
                    "\tp.DELETED = 0\n" +
                    "\t)\n" +
                    "\tAND" +
                    "\tr.DOCTOR_ID = #{doctorId}\n" +
                    "\tAND (\n" +
                    "\t\tr.CARE_ID = p.CARE_ID\n" +
                    "\t\tOR\n" +
                    "\t\tr.IDENT_ID = p.IDENT_ID\n" +
                    ")",
            "</script>"
    })
    List<Map<String, Object>> queryRegistrationPatientBaseInfo(@Param("doctorId") String doctorId);

    /**
     * 修改挂号状态
     * @param registrationId
     * @param statusCode
     * @return
     */
    @Update({
            "UPDATE REGISTRATION SET REGISTER_STATUS = #{statusCode} WHERE id" +
                    " = #{registrationId}"
    })
    Integer updateStatusById(@Param("registrationId")String registrationId,
                             @Param("statusCode")Integer statusCode);
}